/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.hs.srm.base.modules.sys.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.base.modules.sys.dao.SysRoleDao;
import com.hs.srm.base.modules.sys.entity.SysDeptEntity;
import com.hs.srm.base.modules.sys.entity.SysRoleEntity;
import com.hs.srm.base.modules.sys.service.SysDeptService;
import com.hs.srm.base.modules.sys.service.SysRoleDeptService;
import com.hs.srm.base.modules.sys.service.SysRoleMenuService;
import com.hs.srm.base.modules.sys.service.SysRoleService;
import com.hs.srm.base.modules.sys.service.SysUserRoleService;
import com.hs.srm.common.utils.PageUtils;

/**
 * 角色
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:45:12
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        // 删除角色
        this.deleteBatchIds(Arrays.asList(roleIds));

        // 删除角色与菜单关联
        this.sysRoleMenuService.deleteBatch(roleIds);

        // 删除角色与部门关联
        this.sysRoleDeptService.deleteBatch(roleIds);

        // 删除角色与用户关联
        this.sysUserRoleService.deleteBatch(roleIds);
    }

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");

        Page<SysRoleEntity> page = this.selectPage(
                new QueryParam<SysRoleEntity>(params).getPage(),
                new EntityWrapper<SysRoleEntity>().like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null,
                                (String) params.get(Constant.SQL_FILTER)));

        for (SysRoleEntity sysRoleEntity : page.getRecords()) {
            SysDeptEntity sysDeptEntity = this.sysDeptService.selectById(sysRoleEntity.getDeptId());
            if (sysDeptEntity != null) {
                sysRoleEntity.setDeptName(sysDeptEntity.getName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRoleEntity role) {
        role.setCreateTime(new Date());
        this.insert(role);

        // 保存角色与菜单关系
        this.sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        // 保存角色与部门关系
        this.sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @DataFilter(tableAlias = "a", subDept = true, user = false)
    public List<SysRoleEntity> selectCustomList(Map<String, Object> params) {
        EntityWrapper<SysRoleEntity> wrapper = new EntityWrapper<>();
        wrapper.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER));
        return this.baseMapper.selectCustomList(wrapper);
    }

    @Override
    public List<SysRoleEntity> findRoleName(Long userId) {
        List<SysRoleEntity> list = this.baseMapper.findRoleName(userId);
        return list;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        SysRoleEntity orginalRole = this.selectById(role.getRoleId());
        role.setCreateTime(orginalRole.getCreateTime());
        this.updateAllColumnById(role);

        // 更新角色与菜单关系
        this.sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        // 保存角色与部门关系
        this.sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

}
